package cn.edu.scujcc;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LivingService {
	@Autowired
	private LivingRepository repo;
	
	public List<Living> getAllBilibili(){
		List<Living> result = null;
		result = repo.findAll();
		return result;
	}
	
	public Living newBilibili(Living b) {
		Living result = null;
		result = repo.save(b);
		return result;
	}
	
	public Living getIntro(User id) {
		Living result = null;
		result = repo.findById(id);
		return result;
	}
	
	public Living getShare(User name) {
		Living result = null;
		Optional<Living> op = repo.findByName(name);
		if(op.isPresent()) {
			result = op.get();
		}
		return result;
	}
	
	public Living getShares(User id) {
		Living result = getShare(id);
		result.setShare(result.getShare()+1);
		repo.save(result);
		return result;
	}
	
	public Living getFollow(User name) {
		Living result = null;
		Optional<Living> op = repo.findByName(name);
		if(op.isPresent()) {
			result = op.get();
		}
		return result;
	}
	
	public Living getFollows(User id) {
		Living result = getShare(id);
		result.setShare(result.getFollow()+1);
		repo.save(result);
		return result;
	}
}
